<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="appkey" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="appkey">
              <div>
                <a-input v-model="model.appkey" placeholder="请输入appkey"></a-input>
                <a-button type="primary" @click="model.appkey = randomString()">randomString</a-button>
              </div>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="appsecrect" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="appsecrect">
              <a-input v-model="model.appsecrect" placeholder="请输入appsecrect"></a-input>
              <a-button type="primary" @click="model.appsecrect = randomString()">randomString</a-button>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="用户名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="appName">
              <a-input v-model="model.appName" placeholder="请输入用户名"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="角色授权" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fileUrl">
              <!-- <j-select-role v-model="model.lastUseInfo" @change="changeMe"/> -->
              <j-select-open-api-role queryParamCode="fileName" v-model="model.fileUrl" @change="changeMe" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remarks">
              <a-input v-model="model.remarks" placeholder="请输入备注"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'OpenApiAppForm',
  components: {
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {
        appkey: "",
        appsecrect: ""
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        appkey: [
          { required: true, message: '请输入appkey!' },
        ],
        appsecrect: [
          { required: true, message: '请输入appsecrect!' },
        ],
        appRole: [
          { required: true, message: '请输入角色权限!' },
          //{validator: this.checkAccount}
        ],
      },
      url: {
        add: "/api/openApiApp/addAuth",
        edit: "/api/openApiApp/editAuth",
        queryById: "/api/openApiApp/queryById"
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    /*checkAccount(rule, value, callback) {
        if (this.model.queryType===0){
            callback();
            return ;
        } else {
          debugger;
          var that = this;
          this.$confirm({
            title: '确认授权',
            content: res.result,
            onOk() {
              callback();
            },
            onCancel() {
              that.loading = true;
              that.model.lastUseInfo = null;
              callback();
            },
          });

        }
    },*/
    add() {
      this.edit(this.modelDefault);
    },
    randomString(e = 32) {
      const t = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678",
        a = t.length;
      let n = "";
      for (let i = 0; i < e; i++) n += t.charAt(Math.floor(Math.random() * a));
      return n
    },
    edit(record) {
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!that.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          httpAction(httpurl, that.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
    changeMe() {
      console.log('you so ...  , change Me')
    },
  }
}
</script>